//
//  MTMarkDetailVC.m
//  met
//
//  Created by 李晓俊 on 2019/6/14.
//  Copyright © 2019 李晓俊. All rights reserved.
//

#import "MTMarkDetailVC.h"
#import <SDCycleScrollView.h>
#import "MTMarkDetailCell.h"
#import "HighLightButton.h"
#import "MTFeedBackVC.h"
@interface MTMarkDetailVC ()<SDCycleScrollViewDelegate>
@property (nonatomic,strong) SDCycleScrollView *cycleScrollView;
@property (nonatomic,strong) HighLightButton *rightBtn;
@property(nonatomic ,strong) UIAlertController *alert;

@end

@implementation MTMarkDetailVC
- (void) setNav {
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"jubao"] style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
    self.navigationItem.rightBarButtonItem = rightBtn;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if ([self.isLook isEqualToString:@"1"]) {
        [self setNav];
    }else {
        [self customNavItem];
    }
    
    [self loadNavBut];
    [self setupTableView:UITableViewStyleGrouped header:NO footer:NO];
    
    NSMutableArray *imgArr = [NSMutableArray new];
    if  (![[NSString stringWithFormat:@"%@",self.model.image1] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image1] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image1]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image2] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image2] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image2]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image3] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image3] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image3]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image4] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image4] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image4]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image5] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image5] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image5]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image6] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image6] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image6]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image7] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image7] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image7]];
    }
   
    if  (![[NSString stringWithFormat:@"%@",self.model.image8] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image8] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image8]];
    }
    if  (![[NSString stringWithFormat:@"%@",self.model.image9] isEqualToString:@"<null>"]&&[[NSString stringWithFormat:@"%@",self.model.image9] length] > 0) {
        [imgArr addObject:[NSString stringWithFormat:@"%@",self.model.image9]];
    }
    
    // 网络加载 --- 创建带标题的图片轮播器
    self.cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0,  kScreenWidth, kScreenWidth)  delegate:self placeholderImage:[UIImage imageNamed:@"placeholder"]];

    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
    self.cycleScrollView.backgroundColor = KWhiteColor;
    //是否循环
    self.cycleScrollView.infiniteLoop = NO;
    //是否自动滚动
    self.cycleScrollView.autoScroll = NO;
    self.cycleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    
    self.cycleScrollView.bannerImageViewContentMode = 1;
//    cycleScrollView2.titlesGroup = titles;
    self.cycleScrollView.currentPageDotColor = KMainColor; // 自定义分页控件小圆标颜色
    self.cycleScrollView.pageDotColor = KWhiteColor;
//    [self.view addSubview:self.cycleScrollView];
    
    self.tableView.tableHeaderView = self.cycleScrollView;
    self.tableView.tableFooterView = [UIView new];
    self.tableView.backgroundColor = KWhiteColor;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
//    self.tableView.rowHeight = UITableViewAutomaticDimension;
//    self.tableView.estimatedRowHeight = 71 ;
    self.tableView.estimatedSectionFooterHeight = 0;
    //采用网络图片
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        self.cycleScrollView.imageURLStringsGroup = imgArr;
    });
    self.view.backgroundColor = KWhiteColor;
}
- (void)loadNavBut {
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
    UILabel *la = [[UILabel alloc] initWithFrame:CGRectMake(44, 5, kScreenWidth-100, 16)];
    [leftView addSubview:la];
    la.text = [NSString stringWithFormat:@"%@",self.model.nickName];
    la.font = SBFONT(15);
    la.textColor = KBlackColor;
    
    UIImageView *dingwei = [[UIImageView alloc] initWithFrame:CGRectMake(44, 26, 8, 10)];
    [leftView addSubview:dingwei];
    dingwei.image = [UIImage imageNamed:@"dingwei2"];
    
    UILabel *address = [[UILabel alloc] initWithFrame:CGRectMake(44+15, 23, kScreenWidth-100, 16)];
    [leftView addSubview:address];
    address.text = [NSString stringWithFormat:@"%@",self.model.address];
    address.font = CLRudeCircleTextFont(12);
    address.textColor = TextDescribeColor;
    
    if ([[NSString stringWithFormat:@"%@",self.model.address] isEqualToString:@""]) {
        dingwei.hidden = YES;
    }
    
    UIImageView *img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 3, 38, 38)];
    [leftView addSubview:img];
    img.layer.cornerRadius = 19.0f;
    img.layer.masksToBounds = YES;
    [img sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.portraitMap]] placeholderImage:[UIImage imageNamed:@"椭圆 1 拷贝 7"]];
//    img.image = [UIImage imageNamed:@"椭圆 1 拷贝 7"];
    
    //间隙
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = 10;
    UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc ]initWithImage:[UIImage imageWithOriginalName:@"返回黑色"] style:UIBarButtonItemStyleDone target:self action:@selector(returnAction)];

    self.navigationItem.leftBarButtonItems = @[leftBtn,fixedSpaceBarButtonItem,[[UIBarButtonItem alloc] initWithCustomView:leftView]];
    
}
- (void)returnAction {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.cycleScrollView adjustWhenControllerViewWillAppera];
}
#pragma mark - 定制导航条内容
- (void) customNavItem {
    self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.navigationController.navigationBar.barTintColor = KBlackColor;
    UIView * right1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 44)];
    
    self.rightBtn = [[HighLightButton alloc] initWithFrame:CGRectMake(0, 11, 23, 22)];
    [right1 addSubview:self.rightBtn];
    
    if ([[NSString stringWithFormat:@"%@",self.model.isPraise] isEqualToString:@"1"]) {
        [self.rightBtn setImage:[UIImage imageNamed:@"yizan"] forState:UIControlStateNormal];
    }else {
        [self.rightBtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
    }
    //    [rightBtn setTitle:@" 关注" forState:UIControlStateNormal];
    [self.rightBtn setTitleColor:KBlackColor forState:UIControlStateNormal];
    self.rightBtn.titleLabel.font = CLRudeCircleTextFont(15);
    [self.rightBtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateHighlighted];
    [self.rightBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    
    //间隙
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = -10;
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"jubao"] style:UIBarButtonItemStyleDone target:self action:@selector(rightClick)];
    self.navigationItem.rightBarButtonItems = @[[[UIBarButtonItem alloc] initWithCustomView:right1],fixedSpaceBarButtonItem,rightBtn];
}



//举报
- (void)rightClick {
    [self.view endEditing:YES];
    if (!self.alert) {
        self.alert = [UIAlertController alertControllerWithTitle:@"请选择举报原因" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"色情低俗" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"政治敏感" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //意见
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"广告" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //意见
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"令人恶心" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //意见
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"违纪违法" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //意见
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"其他" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //意见
            MTFeedBackVC *vc = GetViewControllerInStoryboard(@"Mine", @"MTFeedBackVC");
            vc.isYinji = @"1";
            vc.yinjiId = [NSString stringWithFormat:@"%@",self.model.imprintId];
            [self.navigationController pushViewController:vc animated:YES];
        }]];
        [self.alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }]];
    }
    [self presentViewController:self.alert animated:true completion:nil];    
}
- (void)rightAction {
   
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    AccountService *acc = [AccountService getObject];
    acc.httpRequestResultBlock = ^(id data,int code){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (code == 1) {
            if ([[NSString stringWithFormat:@"%@",self.model.isPraise] isEqualToString:@"1"]) {
                self.model.isPraise = @"0";
                [self.view maketishiToast:@"取消成功"];
                [self.rightBtn setImage:[UIImage imageNamed:@"zan"] forState:UIControlStateNormal];
            }else {
                self.model.isPraise = @"1";
                [self.view maketishiToast:@"点赞成功"];
                [self.rightBtn setImage:[UIImage imageNamed:@"yizan"] forState:UIControlStateNormal];
            }
        }else {
            [self.view maketishiToast:data];
        }
    };
    NSString *isP = @"";
    if ([[NSString stringWithFormat:@"%@",self.model.isPraise] isEqualToString:@"1"]) {
        isP = @"1";
    }else {
        isP = @"0";
    }
    [acc addOrRemoveIP:[NSString stringWithFormat:@"%@",self.model.imprintId] type:isP];
    
//   addOrRemoveIP:(NSString *)imprintId type:(NSString *)type;
}
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
    NSLog(@"%ld",(long)index);
}

#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString * str =  [NSString stringWithFormat:@"%@",self.model.content];
    NSString *handledStr = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    CGFloat height =  [UnviersalTool getSpaceLabelHeight:handledStr withFont:SBFONT(15) withWidth:kScreenWidth-32 withLineSpacing:3];

    NSString * str2 =  [NSString stringWithFormat:@"%@",self.model.title];
    NSString *handledStr2 = [str2 stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    CGFloat height2 =  [UnviersalTool getSpaceLabelHeight:handledStr2 withFont:SBFONT(17) withWidth:kScreenWidth-32 withLineSpacing:3];
    
    if (height*1.2 + height2 * 1.2 + 1.5 + 51 > 101) {
        return height*1.2 + height2 * 1.2 + 51;
    }else {
        return 101;
    }
    
//    return UITableViewAutomaticDimension;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MTMarkDetailCell *cell = [MTMarkDetailCell cellWithTableView:tableView];
//    cell.content.text = [NSString stringWithFormat:@"%@\n%@",self.model.title,self.model.content];
    cell.tagLabel.text = [NSString stringWithFormat:@"%@",self.model.tab];
    if ([[NSString stringWithFormat:@"%@",self.model.tab] isEqualToString:@""]) {
        cell.tagImage.hidden = YES;
    }else {
        cell.tagImage.hidden = NO;
    }
    
    NSString *str = @"";
    if (self.model.content.length > 0) {
        str = [NSString stringWithFormat:@"%@",self.model.content];
    }
    NSString *handledStr = [str stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    
    [UnviersalTool setLabelSpace:cell.content withValue:handledStr withFont:SBFONT(15) withLineSpacing:3];

    NSString *str2 = @"";
    if (self.model.title.length > 0) {
        str2 = [NSString stringWithFormat:@"%@",self.model.title];
    }else {
        str2 = @" ";
    }
    NSString *handledStr2 = [str2 stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"];
    [UnviersalTool setLabelSpace:cell.titleL withValue:handledStr2 withFont:BFONT(17) withLineSpacing:3];

    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *v = [UIView new];
    return v;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01f;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [UIView new];
    return view;
}

@end
